package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-14 15:35
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.UserAttentionRecordMapper;
import com.datang.pet.mapper.UserMessageMapper;
import com.datang.pet.mapperExtend.UserAttentionRecordMapperExtend;
import com.datang.pet.mapperExtend.UserInfoMapperExtend;
import com.datang.pet.pojo.UserAttentionRecord;
import com.datang.pet.pojo.UserAttentionRecordExample;
import com.datang.pet.pojo.UserMessage;
import com.datang.pet.pojo.UserMessageExample;
import com.datang.pet.service.AttentionService;
import com.datang.pet.service.MessageService;
import com.datang.pet.vo.AddAttentionRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AttentionServiceImpl implements AttentionService {

    @Autowired
    private UserAttentionRecordMapper userAttentionRecordMapper;

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private MessageService messageService;


    @Autowired
    private UserAttentionRecordMapperExtend userAttentionRecordMapperExtend;

    @Autowired
    private UserInfoMapperExtend userInfoMapperExtend;


    @Transactional(rollbackFor = Exception.class)
    public void addAttention(AddAttentionRequest addAttentionRequest) throws Exception {
        String type = addAttentionRequest.getType();
        if (type.equals("1")) {
            //关注
            UserAttentionRecord userAttentionRecord = new UserAttentionRecord();
            userAttentionRecord.setId(StringUtils.getId());
            userAttentionRecord.setUserId(addAttentionRequest.getUserId());
            userAttentionRecord.setFansId(addAttentionRequest.getFansId());
            userAttentionRecord.setAttentionDate(new Date());
            try {
                userAttentionRecordMapper.insert(userAttentionRecord);
            } catch (Exception e) {
                throw new Exception("关注失败");
            }


            //被关注人 个人信息粉丝数+1
            userInfoMapperExtend.contrFansQuantity(addAttentionRequest.getUserId(), "1");
            //关注人 个人信息关注数+1
            userInfoMapperExtend.contrConcernQuantity(addAttentionRequest.getFansId(), "1");


            List<UserMessage> userMessages = null;
            try {
                UserMessageExample userMessageExample = new UserMessageExample();
                UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
                //消息是3
                criteria.andMessageTypeEqualTo("3");
                //A的id
                criteria.andUserIdEqualTo(addAttentionRequest.getUserId());
                //model B 的id
                criteria.andModelIdEqualTo(addAttentionRequest.getFansId());
                userMessages = userMessageMapper.selectByExample(userMessageExample);
            } catch (Exception e) {
                throw new Exception("查询用户消息失败.粉丝处");
            }

            if (userMessages.isEmpty()) {
                //没有查询到这样的消息,说明这个B用户从来没有关注A用户
                //生成消息
                try {
                    UserMessage userMessage = new UserMessage();
                    userMessage.setId(StringUtils.getId());
                    userMessage.setIsRead("0");
                    userMessage.setMessageType("3");
                    userMessage.setUserId(addAttentionRequest.getUserId());
                    userMessage.setModelId(userAttentionRecord.getFansId());
                    userMessage.setMessageDate(new Date());
                    userMessageMapper.insert(userMessage);
                } catch (Exception e) {
                    throw new Exception("添加消息失败,粉丝处");
                }
                //发送消息
                messageService.getMessage(addAttentionRequest.getUserId(), "3");
            }
        } else {
            try {
                //取消关注
                UserAttentionRecordExample userAttentionRecordExample = new UserAttentionRecordExample();
                UserAttentionRecordExample.Criteria criteria = userAttentionRecordExample.createCriteria();
                criteria.andUserIdEqualTo(addAttentionRequest.getUserId());
                criteria.andFansIdEqualTo(addAttentionRequest.getFansId());
                userAttentionRecordMapper.deleteByExample(userAttentionRecordExample);
            } catch (Exception e) {
                throw new Exception("取消关注失败");
            }

            //被关注人 个人信息粉丝数-1
            userInfoMapperExtend.contrFansQuantity(addAttentionRequest.getUserId(), "2");
            //关注人 个人信息关注数-1
            userInfoMapperExtend.contrConcernQuantity(addAttentionRequest.getFansId(), "2");

        }
    }

}
